package SparkRDD.RDD算子.Transformations.分区操作

import org.apache.spark.{SparkConf, SparkContext}
import org.junit.Test

/*
  * repartition --- 表示重新分区，可大可小 ==> shuffle默认为true
  *  coalesce   --- 表示重分区时，不可增大 ==> shuffle默认为false，修改为true即可增大
 */
class divideAreaTest {

  val conf = new SparkConf().setMaster("local[6]").setAppName("sortBy")
  val sc   = new SparkContext(conf)

  @Test
  def repartitionTest: Unit ={

    val rdd = sc.parallelize(Seq(1,2,3,4,5,6,7,8),2)
    println(rdd.repartition(4).partitions.size) // 4
    println(rdd.repartition(1).partitions.size) // 1

  }

  @Test
  def coalesceTest: Unit ={

    val rdd = sc.parallelize(Seq(1,2,3,4,5,6,7,8),2)
    println(rdd.coalesce(4).partitions.size) // 不是4，还是2
    println(rdd.coalesce(4,shuffle = true).partitions.size) // 4
    println(rdd.coalesce(1).partitions.size) // 1

  }

}
